package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.Region;
import com.tbit.common.entity.pojo.dto.BarChartRepairDTO;
import com.tbit.common.entity.pojo.vo.DateResult;
import com.tbit.uqbike.object.pojo.statistics.ExportPowerLog;
import com.tbit.uqbike.object.pojo.statistics.RepairLog;
import com.tbit.uqbike.service.business.AccountUserService;
import com.tbit.uqbike.service.business.RepairLogService;
import com.tbit.uqbike.webmanager.dao.core.AccountUserDao;
import com.tbit.uqbike.webmanager.dao.log.RepairLogDao;
import com.tbit.uqbike.webmanager.util.StringUtil;
import com.tbit.utils.DateTimeUtil;
import com.tbit.utils.NullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: chen
 * @Description: 维修记录
 * @Date: 创建于 16:54 2020/3/13
 */
@Service
public class RepairLogServiceImpl implements RepairLogService {

    @Autowired
    private RepairLogDao repairLogDao;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private AccountUserDao accountUserDao;

    @Override
    public List<List<DateResult>> statisticsByTime(BarChartRepairDTO barChartRepairDTO) {

        List<List<DateResult>> totalList = new ArrayList<>();

        //获取运营区的所有分区id
        List<Region> list = accountUserDao.getRegionId(barChartRepairDTO.getAccountId());
        //获取区域维修数据
        barChartRepairDTO.setStartDate(new Date(barChartRepairDTO.getStartTime()));
        barChartRepairDTO.setEndDate(new Date(barChartRepairDTO.getEndTime()));
        List<DateResult> results = repairLogDao.statisticsByTime(barChartRepairDTO);
        //获取所有分区数据
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> findTimeList = DateTimeUtil.findDaysStr(simpleDateFormat.format(new Date(barChartRepairDTO.getStartTime())), simpleDateFormat.format(new Date(barChartRepairDTO.getEndTime())));
        barChartRepairDTO.setRegionIds(list.stream().map(Region::getRegionId).collect(Collectors.toList()));
        List<DateResult> regionDateList = repairLogDao.statisticsByTimeAndRegionId(barChartRepairDTO);
        //构造分区Map
        Map<Integer, List<DateResult>> map = new HashMap<>();
        for (DateResult dateResult : regionDateList) {
            List<DateResult> dateResults = map.get(dateResult.getRegionId());
            if (CollectionUtil.isNotEmpty(dateResults)) {
                dateResults.add(dateResult);
            } else {
                dateResults = new ArrayList<>();
                dateResults.add(dateResult);
                map.put(dateResult.getRegionId(), dateResults);
            }
        }
        totalList.add(results);
        for (Region region : list) {
            List<DateResult> dateResults = CollectionUtil.isEmpty(map.get(region.getRegionId())) ? new ArrayList<>() : map.get(region.getRegionId());
            //获取分区日期差值
            List<String> reduceRegion = findTimeList.stream().filter(item -> !dateResults.contains(item)).collect(Collectors.toList());
            //数据补0
            for (String date : reduceRegion) {
                DateResult dateResult = new DateResult();
                dateResult.setDate(date)
                        .setTotal(0);
                dateResults.add(dateResult);
            }
            for (DateResult result : dateResults) {
                result.setRegionId(region.getRegionId());
                if (region.getRegionId().equals(result.getRegionId())) {
                    result.setRegionName(region.getRegionName());
                }
            }
            totalList.add(dateResults);
        }
        totalList.removeIf(NullUtils::isEmpty);
        return totalList;
    }

    @Override
    public List<RepairLog> queryPage(Integer accountId, String machineNO, String userCode, Integer accountUserId,
                                     Long startTime, Long endTime, List<Integer> regionIdList, Integer pageNO, Integer rowCount) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("machineNO", machineNO);
        map.put("userCode", userCode);
        map.put("accountUserId", accountUserId);
        map.put("startTime", new Date(startTime));
        map.put("endTime", new Date(endTime));
        map.put("pageNO", (pageNO - 1) * rowCount);
        map.put("rowCount", rowCount);
        map.put("regionIds", regionIdList);
        List<RepairLog> logs = repairLogDao.queryPage(map);

        List<AccountUser> list = accountUserService.getByType(accountId, false);
        if (list.size() > 0) {
//      *填充运维人员数据
            for (AccountUser a : list) {
                for (RepairLog e : logs) {
                    if (a.getAccountUserId().equals(e.getAccountUserId())) {
                        e.setUserName(a.getName());
                        e.setPhone(a.getPhone());
                    }
                }
            }
        }
        return logs;
    }


    @Override
    public Integer queryCount(Integer accountId, String machineNO, String userCode,
                              Integer accountUserId, Long startTime, Long endTime, List<Integer> regionIdList) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("userCode", userCode);
        map.put("machineNO", machineNO);
        map.put("accountUserId", accountUserId);
        map.put("startTime", new Date(startTime));
        map.put("endTime", new Date(endTime));
        map.put("regionIds", regionIdList);
        return repairLogDao.queryCount(map);
    }

    @Override
    public List<RepairLog> exportByTime(Integer accountId, Integer accountUserId, Long startTime,
                                        Long endTime, String regionIds) {
        Map<String, Object> map = new HashMap<>();
        map.put("startTime", new Date(startTime));
        map.put("accountId", accountId);
        map.put("endTime", new Date(endTime));
        map.put("accountUserId", accountUserId);
        map.put("regionIds", StringUtil.getIntegerList(regionIds));
        List<RepairLog> logs = repairLogDao.exportByTime(map);
        List<AccountUser> list = accountUserService.getByType(accountId, false);
        if (list.size() > 0) {
//      *填充运维人员数据
            for (AccountUser a : list) {
                for (RepairLog e : logs) {
                    if (a.getAccountUserId().equals(e.getAccountUserId())) {
                        e.setUserName(a.getName());
                        e.setPhone(a.getPhone());
                    }
                }
            }
        }

        return logs;
    }

    @Override
    public List<ExportPowerLog> getByType(Integer accountId, Integer accountUserId, Long startTime, Long endTime, Integer rowCount, Integer pageNO, List<Integer> regionIdList, Integer type) {
        PageHelper.startPage(pageNO, rowCount);
        HashMap<String, Object> params = new HashMap<>();
        params.put("accountId", accountId);
        params.put("accountUserId", accountUserId);
        params.put("startTime", new Date(startTime));
        params.put("endTime", new Date(endTime));
        params.put("pageNO", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);
        params.put("regionIds", regionIdList);
        List<ExportPowerLog> logs = new ArrayList<>();
        if (type == 0) {
            logs = repairLogDao.selectByType(params);
            List<AccountUser> list = accountUserService.getByType(accountId, false);
            if (list.size() > 0) {
                /**填充运维人员数据*/
                for (AccountUser a : list) {
                    for (ExportPowerLog e : logs) {
                        if (a.getAccountUserId().equals(e.getAccountUserId())) {
                            e.setName(a.getName());
                            e.setPhone(a.getPhone());
                        }
                    }
                }
            }
        } else if (type == 1) {
            logs = repairLogDao.selectByTypeAndRegionId(params);
            /**查询某个运营区下所有的分区id*/
            List<Region> regionList = accountUserDao.getRegionId(accountId);
            Map<Integer, Region> map = new HashMap<>();
            if (regionList != null) {
                for (Region region : regionList) {
                    map.put(region.getRegionId(), region);
                }
            }

            for (ExportPowerLog log : logs) {
                Region region = map.get(log.getRegionId());
                if (region != null) {
                    log.setRegionName(region.getRegionName());
                }
            }
        }
        return logs;
    }
}
